/**
 * Copyright (c) 2019 AsHacker All rights reserved.
 * <p>
 * http://www.ashacker.com
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.renren.common.utils.R;
import io.renren.modules.lucky.service.AppSysOpertionService;
import io.renren.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * APP模块分析
 *
 * @author Chen chenchen@ashacker.com
 */
@RestController
@RequestMapping("/app")
@Api("APP模块分析")
public class AppAnalyseController extends AbstractController {

    @Autowired
    private AppSysOpertionService appSysOpertionService;

    /**
     * 统计用户事件习惯类别
     * type=1 点击事件
     */
    @PostMapping("addUserAnalyse")
    @ApiOperation("统计用户事件")
    public R addUserAnalyse(@RequestParam String token, @RequestBody String json) {

        logger.info("addUserAnalyse方法执行开始：传入参数为   " + json);
        JSONObject jobj = JSON.parseObject(json);

        String userId = jobj.get("user_id").toString();
        String operationCode = jobj.get("operation_code").toString();
        String operationName = jobj.get("operation_name").toString();

        int num = 0;
        try {
            // 插入数据信息
            num = appSysOpertionService.addAnalyseInfo(userId, operationCode, operationName, token);
        } catch (Exception e) {
            logger.error("保存信息失败,错误原因:" + e.getMessage(), e);
            e.printStackTrace();
        }

        Map<String, Object> map = new HashMap<>();
        map.put("num", num);

        logger.info("addUserAnalyse方法执行结束：返回值为   " + JSON.toJSONString(map));

        return R.ok(map);

    }

}
